/*
 * @lc app=leetcode.cn id=202 lang=cpp
 *
 * [202] 快乐数
 */

// @lc code=start
class Solution {
public:
    int Cal(int n)
    {
        int sum = 0;
        int num = 0;
        while(n > 0)
        {
            num  = n % 10;
            sum += num * num;
            n = n / 10;     
        }
        return sum;
    }
    bool isHappy(int n) {
        int sum = Cal(n);
        set<int> s;//用来判断是否重复
        while(s.find(sum) == s.end())
        {
            s.insert(sum);
            if(sum == 1) {
                break; 
            }else {
                sum = Cal(sum);
            }
        }
        if(sum==1)
            return true;
        else
            return false;
    }
};
// @lc code=end

